import { createRouter, createWebHistory } from "vue-router";

// 自动导入modules文件夹下的所有js文件
// import.meta.globEager 为直接引入  vite
const modules = import.meta.globEager("./modules/**/*.js");
console.log("modules", modules);
// Object.keys()
// 传入对象，返回对象的属性名，就是对象的属性键名
// 传入字符串，返回索引
// 传入数组，返回索引
// 传入构造函数 返回空数组或者属性名
const moduleList = [];
Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  moduleList.push(...modList);
});
// 动态路由
const asyncRouterList = [...moduleList];

// 静态路由
const defaultRouterList = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/",
    redirect: "/dashboard/base",
  },
  {
    path: "/image",
    component: () => import("@/views/image/index.vue"),
  },
  {
    path: "/progress",
    component: () => import("@/views/progress/index.vue"),
  },
  {
    path: "/:w+",
    name: "404page",
    redirect: "/result/404",
  },
];

const allRoutes = [...asyncRouterList, ...defaultRouterList];

const router = createRouter({
  history: createWebHistory(),
  routes: allRoutes,
  scrollBehavior() {
    return {
      el: "#app",
      top: 0,
      behavior: "smooth",
    };
  },
});
export default router;
